package com.bnzj.cloud.business.core.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import org.springframework.stereotype.Service;

import com.bnzj.cloud.business.core.persistence.repository.SysUserRepository;
import com.bnzj.core.export.Column;
import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.util.FbConstant;

@Service
public class SysUserService extends BaseService<SysUserRepository, SysUser, Long> {

    private static final List<Column> columns = new ArrayList<Column>();

    static {
        columns.add(new Column("account", "工号", 0));
        columns.add(new Column("cname", "中文名", 1));
        columns.add(new Column("ename", "英文名", 2));
        columns.add(new Column("email", "邮箱", 3));
        columns.add(new Column("sex", "性别", 4, t -> "1".equals(t) ? "男" : "女"));
        columns.sort(null);
    }

    //@Cacheable
    public SysUser getAdmin() {
        return repository.findByAccount(FbConstant.ADMIN);
    }

    public SysUser findByAccount(String account) {
        return repository.findByAccount(account);
    }
    public Set<String> findAllAccount() {
        return repository.findAllAccount();
    }

    public Set<String> findAllAccountByEnabled() {
        return repository.findAllAccountByEnabled();
    }

    public List<SysUser> findAllByAccount(List<String> accountList) {
        return repository.findAllByAccount(accountList);
    }

    public Long findIdByAccount(String account) {
        return repository.findIdByAccount(account);
    }

    @Transactional
    public int disable(List<String> accountList) {
        return repository.disable(accountList);
    }

    public SysUser eagerlyFindByAccount(String username) {
        return repository.eagerlyFindByAccount(username);
    }

    public static List<Column> getColumnList() {
        return Collections.unmodifiableList(columns);
    }

    public static QueryHelpBean getQueryHelpBean() {
        String selectSQL = " select u.* ";
        String whereSQL = " FROM bnz_sys_user u where  u.account!='admin' ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysUser.class);
        return queryBean;
    }

    @Transactional
    public boolean updateUserEnableById(Long id, boolean sign) {
        repository.updateUserEnableById(id, sign);
        return true;
    }

    public String findEmailByAccount(String creator) {
        return repository.findEmailByAccount(creator);
    }

    /**
     * @param deptId
     * @param account
     * @param cname
     * @param ename
     * @param pageable
     * @return
     */
    public static QueryHelpBean getQueryHelpBeanByDeptId() {
    	String selectSQL ="select distinct a.id, a.account, a.cname, a.email, a.sex, a.phone, a.ename,a.dept_id, a.password, a.personnel, a.enabled, a.creator, a.create_time, a.modify_by, a.modify_time, a.creator_name, a.modify_name, a.type, a.link_id,b.full_name v_dept_full_name ";
    	String whereSQL =" from bnz_sys_user a,bnz_sys_department b,bnz_sys_department c where  a.dept_id=b.id and 1=1 and a.enabled= 1 and b.full_name like CONCAT(c.full_name,\"%\") ";
    	//        String selectSQL = " select a.*  ";
//        String whereSQL = " from bnz_sys_user a join bnz_sys_department b on  a.dept_id=b.id where 1=1 and a.enabled= 1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysUser.class);
        return queryBean;
    }
    
    public static QueryHelpBean getQueryHelpBeanByAccount() {
    	String selectSQL ="select distinct a.id, a.account, a.cname, a.email, a.sex, a.phone, a.ename,a.dept_id, a.password, a.personnel, a.enabled, a.creator, a.create_time, a.modify_by, a.modify_time, a.creator_name, a.modify_name, a.type, a.link_id,b.full_name v_dept_full_name ";
    	String whereSQL =" from bnz_sys_user a,bnz_sys_department b,bnz_sys_department c where  a.dept_id=b.id and 1=1 and a.enabled= 1  and a.dept_id=c.id and b.full_name like CONCAT(c.full_name,\"%\") ";
    	//        String selectSQL = " select a.*  ";
//        String whereSQL = " from bnz_sys_user a join bnz_sys_department b on  a.dept_id=b.id where 1=1 and a.enabled= 1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, SysUser.class);
        return queryBean;
    }


    @Transactional(rollbackOn = Exception.class)
    public void updatePassword(String password,String account) {
        this.repository.updatePassword(password,account);
    }
    
    @Transactional
	public SysUser newSave(SysUser entity) {
		repository.newSave(entity);
		return entity;
	}
    
    public SysUser findByIdk3(String idk3) {
        return repository.findByIdk3(idk3);
    }

    public List<String> findCodeByPersonnel(Boolean personnel){
    	return repository.findCodeByPersonnel(personnel);
    }
    
    @Transactional
    public void disPersonnel(String idk3, String modifyBy, String modifyName) {
    	repository.disPersonnel(idk3, modifyBy, modifyName, new Date());
    }
}
